// createRouter：创建router实例对象
// createWebHistory：创建history模式的路由

import { createRouter, createWebHistory } from 'vue-router'

import Login from '@/views/Login/index.vue'
import Layout from '@/views/Layout/index.vue'
import Home from '@/views/Home/index.vue'
import Category from '@/views/Category/index.vue'
//导入订单支付页面
import Pay from '@/views/Pay/PayBack.vue'
import Member from '@/views/Member/index.vue'

//导入checkout
import Checkout from '@/views/Checkout/index.vue'
//二级路由
import SubCategory from '@/views/SubCategory/index.vue'
import UserOrder from '@/views/Member/components/UserOrder.vue'
import UserInfo from '@/views/Member/components/UserInfo.vue'
import UserAddress from '@/views/Member/components/UserAddress.vue'

import Detail from '@/views/Detail/index.vue'
//导入购物车列表页面
import CartList from '@/views/CartList/index.vue'

// 路由
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  // path和component对应关系的位置
  routes: [
    {
      path: '/',
      component: Layout,
      children: [
        {
          path: '',
          component: Home
        },
        {
          path: 'categories/:id',
          component: Category
        },
        {
          path: 'category/sub/:id',
          name: 'subCategory',
          component: SubCategory
        },
        {
          path: 'goods/:id',
          component: Detail
        },
        {
          path: 'cartlist',
          component: CartList
        },
        {
          path: 'checkout',
          component: Checkout
        },
        {
          path: 'payback',
          component: Pay
        },
        {
          path: 'member',
          component: Member,
          children: [
            {
              path: '',
              component: UserInfo
            },
            {
              path: 'order',
              component: UserOrder
            },
            {
              path: 'address',
              component: UserAddress
            }
          ]
        }
      ]
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/admin',
      component: () => import('@/views/Admin/Layout.vue'),
      children: [
        { path: '', name: 'admin', component: () => import('@/views/Admin/Home.vue') }, // 首页
        {
          path: '/productlist',
          name: 'productlist',
          component: () => import('../views/Admin/ProductList.vue')
        },
        {
          path: 'productadd',
          name: 'productadd',
          component: () => import('../views/Admin/ProductAdd.vue')
        },
        {
          path: 'productedit/:id',
          name: 'productedit',
          component: () => import('../views/Admin/ProductAdd.vue')
        },
        {
          path: 'userlist',
          name: 'userlist',
          component: () => import('../views/Admin/UserList.vue')
        },
        {
          path: 'orderlist',
          name: 'orderlist',
          component: () => import('../views/Admin/OrderList.vue')
        }
      ]
    }
  ],
  //路由跳转时，滚动条会自动滚动到顶部
  scrollBehavior() {
    return {
      top: 0
    }
  }
})

export default router
